<template>
  <el-dialog :visible.sync="visible" :close-on-click-modal="false" :title="$tt('预警设置')" custom-class="dialog_auto">


    <el-form label-width="auto" label-position="right" ref="Form" :model="form" :rules="rules" class="common-form-row">
      <el-row>
        <el-col :span="1">
          <el-form-item :label="$tt('默认预警库存数')" prop="defaultSafetyNum">
            <ex_super_input v-model="form.defaultSafetyNum" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="3">
          <el-form-item :label="$tt('各仓预警库存数')" prop="safetyStockSettingskuItemList">
            <ex_super_table :data="form.safetySettingDetailList" :model="warehoustModel" height="400">

              <template v-slot:table_before>
                <el-table-column :label="$tt('label_sku_name')" prop="skuName" width="150" show-overflow-tooltip></el-table-column>
              </template>
            </ex_super_table>
          </el-form-item>
        </el-col>

      </el-row>
      <el-row>
        <el-col :span="1">
          <el-form-item :label="$tt('预警提醒方式')" prop="stockWarningType">
            <ex_super_input v-model="form.stockWarningType" type="select" enum="StockWarningTypeEnum" />
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>

    <span slot="footer" class="dialog_footer">
      <el-button @click="visible = false">{{ $tt('label_cancel') }}</el-button>
      <el-button type="primary" @click="submitBefore">{{ $tt('label_submit') }}</el-button>
    </span>

  </el-dialog>
</template>

<script>
const default_obj = {
  defaultSafetyNum: null,
  warehouseList: [],
  safetySettingDetailList: [],
}
export default {

  components: {},
  data() {
    return {
      visible: false,
      tableVisible: false,
      form: _.cloneDeep(default_obj),
      rules: {
        stockWarningType: { required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' },
        defaultSafetyNum: {
          required: true, trigger: 'blur',
          validator: (r, v, cb) => {
            if (_.isEmpty(v) || !this.$reg.PureNumber(v)) return cb(this.$tt('tip_only_positive_pure_decimal'))
            cb()
          }
        },
        safetySettingDetailList: {
          required: true, trigger: 'blur',
          validator: (r, v, cb) => {
            let check = true
            v.forEach(it => {

              this.$set(it, 'error', {})
              it.setting.forEach(wh => {
                if (!_.isEmpty(wh.safetyNum) && !this.$reg.PureNumber(wh.safetyNum)) {
                  this.$set(it.error, wh.warehouseId, this.$tt('tip_only_positive_pure_decimal'))
                  check = false
                }
              })
            })
            if (check) {
              cb()
            } else {
              cb(this.$tt('tip_detail_check_fail'))
            }
          }
        },
      },
      warehoustModel: [],
    }
  },
  props: {


  },

  computed: {

  },
  methods: {

    show(items,) {
      this.visible = true

      this.form = _.cloneDeep(default_obj)


      // const data = {
      //   "customerId": "1675706956095447042",
      //   "warehouseList": [
      //     {
      //       "agentId": "1645766967455444993",
      //       "warehouseId": "1673998828287057921",
      //       "warehouseCode": "ffsa",
      //       "warehouseName": "ffsa"
      //     },
      //     {
      //       "agentId": "1645766967455444993",
      //       "warehouseId": "1675707889244774401",
      //       "warehouseCode": "SZ-001",
      //       "warehouseName": "深圳中转仓"
      //     },
      //     {
      //       "agentId": "1645766967455444993",
      //       "warehouseId": "1675710095268323329",
      //       "warehouseCode": "YW-001",
      //       "warehouseName": "义乌中转仓"
      //     }
      //   ],
      //   "safetyStockSettingskuItemList": [
      //     {
      //       "skuId": "1677202794658525186",
      //       "skuOnlyCode": "MEILIN-qrqrq",
      //       "skuName": "qweq",
      //       "safetyStockSettingList": [
      //         {
      //           "status": "ENABLE",
      //           "agentId": "1645766967455444993",
      //           "warehouseId": "1673998828287057921",
      //           "customerId": "1675706956095447042",
      //           "customerCode": "MEILIN",
      //           "customerName": "梅林测试",
      //           "skuId": "1677202794658525186",
      //           "skuOnlyCode": "MEILIN-qrqrq",
      //           "skuName": "qweq",
      //           "safetyStockNum": 100,
      //           "warehouseCode": "ffsa",
      //           "warehouseName": "ffsa",
      //           "id": "1677211078138638337",
      //           "gmtCreate": "1688713256771",
      //           "gmtModify": "1688713256771",
      //           "creator": "system",
      //           "modifier": "system",
      //           "isDelete": "N"
      //         },
      //         {
      //           "agentId": "1645766967455444993",
      //           "warehouseId": "1675707889244774401",
      //           "customerId": "1675706956095447042",
      //           "customerCode": "MEILIN",
      //           "customerName": "梅林测试",
      //           "skuId": "1677202794658525186",
      //           "skuOnlyCode": "MEILIN-qrqrq",
      //           "skuName": "qweq",
      //           "warehouseCode": "SZ-001",
      //           "warehouseName": "深圳中转仓"
      //         },
      //         {
      //           "agentId": "1645766967455444993",
      //           "warehouseId": "1675710095268323329",
      //           "customerId": "1675706956095447042",
      //           "customerCode": "MEILIN",
      //           "customerName": "梅林测试",
      //           "skuId": "1677202794658525186",
      //           "skuOnlyCode": "MEILIN-qrqrq",
      //           "skuName": "qweq",
      //           "warehouseCode": "YW-001",
      //           "warehouseName": "义乌中转仓"
      //         }
      //       ]
      //     }
      //   ]
      // }
      this.$ajax({
        url: "/oms-api/client/OmsSafetyStockSettingClientRpc/querySafetyStockSetting.json",
        method: 'post', type: 'json',
        loading: true,
        data: {
          skuIdList: items.map(it => it.id)
        },
        success: (res) => {
          if (res.data.safetyStockSettingskuItemList && res.data.warehouseList) {
            res.data.safetySettingDetailList = items.map(sku => {
              return {
                skuId: sku.id,
                skuName: sku.skuName,
                skuBarcode: sku.skuBarcode,
                skuOnlyCode: sku.skuOnlyCode,
                setting: res.data.warehouseList.map(wh => {
                  let safetyNum = ''
                  const now_sku = _.find(res.data.safetyStockSettingskuItemList, it => it.skuId == sku.id)
                  if (now_sku) {
                    const now_w = _.find(now_sku.safetyStockSettingList, it => it.warehouseId == wh.warehouseId)
                    if (now_w) safetyNum = now_w.safetyStockNum
                  }
                  return {
                    warehouseId: wh.warehouseId,
                    warehouseName: wh.warehouseName,
                    warehouseCode: wh.warehouseCode,
                    safetyNum,
                  }
                })
              }
            })
            this.form = Object.assign(_.cloneDeep(default_obj), res.data)

            this.warehoustModel = res.data.warehouseList.map(it => {
              return {
                label: it.warehouseName, 'min-width': 120, key: it.warehouseId,
                component: ({ row }) => {
                  const item = _.find(row.setting, setting => setting.warehouseId == it.warehouseId)
                  return {
                    value: (item && item.safetyNum) || '',
                    on: {
                      input: e => {
                        if (item) this.$set(item, 'safetyNum', e)
                      }
                    },
                  }
                }
              }
            })
            this.tableVisible = false
            this.$nextTick(() => {
              this.tableVisible = true
            })
          }else{
            this.$message.error(this.$tt('tip_data_load_fail'))
          }


        },
      })
    },
    hide() {
      this.visible = false
    },

    submitBefore() {
      this.$refs['Form'].validate((valid) => {
        if (valid) {
          this.submit()
        } else {
          this.$message.error(this.$tt('form_validate_error'))
          return false;
        }
      });
    },
    submit() {
      const { warehouseList, safetyStockSettingskuItemList, safetySettingDetailList,...data } = this.form
      const list = []
      safetySettingDetailList.forEach(item => {
        item.setting.forEach(it => {
          list.push({
            warehouseId: it.warehouseId,
            skuId: item.skuId,
            safetyNum: it.safetyNum,
          })
        })
      })
      data.safetySettingDetailList = list.filter(it => this.$reg.PureNumber(it.safetyNum))
      this.$ajax({
        url: "/oms-api/client/OmsSafetyStockSettingClientRpc/saveSafetyStockSetting.json",
        method: 'post', type: 'json',
        loading: true,
        data: data,
        success: (res) => {
          this.$message.success(this.$tt('tip_operate_success'))
          this.hide()
        },
      })
    },
  }
}
</script>
